package com.hanj.myserver.utils;

/**
 * 统一整个项目中Ajax请求返回的结果（未来也可以用于分布式架构各个模块间调用时返回统一类型）
 *
 * @param <T>
 * @author 一叶知秋
 */
public class Result<T> {

    public static final String SUCCESS = "SUCCESS";
    public static final String FAILED = "FAILED";

    private String result;

    private String msg;

    private T data;

    /**
     * 请求处理成功且不需要返回数据时使用的工具方法
     */
    public static <Type> Result<Type> successWithoutData(String msg) {

        return new Result<>(SUCCESS, msg, null);
    }

    public static <Type> Result<Type> successWithData(String msg, Type data) {

        return new Result<>(SUCCESS, msg, data);
    }

    public static <Type> Result<Type> failed(String msg) {

        return new Result<>(FAILED, msg, null);
    }

    public Result() {

    }

    public Result(String result, String msg, T data) {

        super();
        this.result = result;
        this.msg = msg;
        this.data = data;
    }

    @Override
    public String toString() {

        return "R{" +
                "result='" + result + '\'' +
                ", msg='" + msg + '\'' +
                ", data=" + data +
                '}';
    }

    public String getResult() {

        return result;
    }

    public void setResult(String result) {

        this.result = result;
    }

    public String getMsg() {

        return msg;
    }

    public void setMsg(String msg) {

        this.msg = msg;
    }

    public T getData() {

        return data;
    }

    public void setData(T data) {

        this.data = data;
    }

}

